package com.piao.springboot_resttemplate.controller;

import com.piao.springboot_resttemplate.entity.Word;
import com.piao.springboot_resttemplate.mapper.WordMapper;
import com.piao.springboot_resttemplate.util.HttpUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {
    //存储预返回页面的结果对象
    private Map<String, Object> result;
    //注入业务对象
    @Resource
    private WordMapper wordMapper;
    //1. 添加单词信息
    @RequestMapping(value = "/Word", method = RequestMethod.POST)
    public Map<String, Object> insert(@RequestBody Word word) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == word) {
            result.put("message", "插入的单词为空");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getEnglish())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getChinese())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }
        wordMapper.insert(word);
        result.put("message", "插入单词成功");
        return result;
    }

    //2. 根据id删除单词信息
    @RequestMapping(value = "/Word/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> delete(@PathVariable Integer id) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == id) {
            result.put("message", "输入的单词id为空");
            return result;
        }
        wordMapper.deleteById(id);
        result.put("message", "删除单词成功");
        return result;
    }

    //3. 根据id更新单词信息
    @RequestMapping(value = "/Word", method = RequestMethod.PUT)
    public Map<String, Object> update(@RequestBody Word word) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == word.getId()) {
            result.put("message", "更新的单词id为空");
            return result;
        }
        if (null == word.getEnglish() || "".equals(word.getEnglish())) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getChinese())) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }
        wordMapper.updateById(word);
        result.put("message", "更新单词成功");
        return result;
    }

    //根据id查询单词信息
    @RequestMapping(value = "/Word/{id}", method = RequestMethod.GET)
    public Map<String, Object> getOne(@PathVariable Integer id) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == id) {
            result.put("message", "传入id为空");
            return result;
        }
        result.put("id", wordMapper.selectById(id).getId());
        result.put("english", wordMapper.selectById(id).getEnglish());
        result.put("chinese", wordMapper.selectById(id).getChinese());
        return result;
    }


//    //根据id查询单词信息
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    public Map<String, Object> test(@PathVariable Integer id) {
//        result = new HashMap<>();
//        //数据校验 判空！
//        if (null == id) {
//            result.put("message", "传入id为空");
//            return result;
//        }
//        result.put("word", HttpUtil.getWordById(id));
//        result.put("message", "查询单词成功");
//        return result;
//    }

}
